unit u_thread_ecoute;

interface

uses IdTCPClient,
     IdContext,
     sysutils,
     IdTCPConnection,
     classes;

type

  ecoute = class(TThread)
    private

      client : TIdTCPClient;
      client_list : Array of string;

  public
    procedure execute; override;
    constructor create(client_ : TIdTCPClient);
  end;

implementation

uses u_engine;

  constructor ecoute.create(client_ : TIdTCPClient);
    begin
      inherited create(false);
      client := client_;
    end;

  procedure ecoute.Execute;
  var
    i,j : integer;
    msg : string;
    s1 : string;
    p1, p2, p3, p4 : integer;
    begin
      try
        while not Terminated do
          begin
            repeat
              msg := client.IOHandler.ReadLn;
            until (msg <> '') or Terminated;
            if msg = '#m' then
              begin
                //Reception d'un msg
                msg := client.IOHandler.ReadLn;
                myplayer.mychat.add_text(msg);
              end
              else if msg = '#p' then
                begin
                  //Actualisation de la position d'un joueur
                  p1 := StrToInt(client.IOHandler.ReadLn);
                  p2 := StrToInt(client.IOHandler.ReadLn);
                  p3 := StrToInt(client.IOHandler.ReadLn);
                  p4 := StrToInt(client.IOHandler.ReadLn);
                  myplayer.myreseau.change_pos(p1, p2, p3, p4);
                end
                else if msg = '#d' then
                  begin
                    //Suppression d'un objet de la map
                    p1 := StrToInt(client.IOHandler.ReadLn);
                    p2 := StrToInt(client.IOHandler.ReadLn);
                    p3 := StrToInt(client.IOHandler.ReadLn);
                    mymesh.suppr_obj(p1, p2, p3, mymesh.dep_eta.etage);
                  end
                  else if msg = '#n' then
                    begin
                      //ajout d'un nouveau joueur
                      s1 := client.IOHandler.ReadLn;
                      myplayer.myreseau.add_player(mymesh.dep_eta.x, mymesh.dep_eta.y, s1);
                    end
                    else if msg = '#s' then
                      begin
                        //suppression d'un joueur
                        s1 := client.IOHandler.ReadLn;
                        myplayer.myreseau.del_player_name(s1);
                      end;
          end;
      except
      On E:Exception do
        myplayer.mychat.add_text(E.Message.ToString);
      end;
    end;


end.

